// pages/order/pay/index.js

const customRequst = require('../../../common/customRequst');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    orderInfo: {},
    payType: [{
        txt: '余额支付',
        type: 'money'
      },
      {
        txt: '微信支付',
        type: 'wechat'
      }
    ],
    rechargePayType: [
      {
        txt: '佣金支付',
        type: 'commission'
      },
      {
        txt: '微信支付',
        type: 'wechat'
      }
    ],

    isSubmit: false,
    formToken: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    let that = this;
    if (option.id) {
      that.setData({
        id: option.id
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    if (that.data.id) {
      customRequst.postRequest('api/contribution/payinfo', {
        id: that.data.id
      }).then(res => {
        if (res.code == 1) {
          that.setData({
            orderInfo: res.data
          })
        } else {
          wx.showToast({
            title: res.msg,
          })
        }
      });
      customRequst.postRequest('api/common/get_form_token', {}).then(res => {
        if (res.code == 1) {
          that.setData({
            formToken: res.data.token
          })
        } else {
          wx.showToast({
            title: res.msg,
          })
        }
      })
    }
  },

  //支付
  orderPay: function (e) {
    let that = this

    let type = e.currentTarget.dataset.type;
    if (!type) {
      return;
    }
    if (!that.data.id) {
      return;
    }
    if (that.data.isSubmit) {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'error'
      });
      return;
    }
    that.setData({
      isSubmit: true
    });
    customRequst.postRequest('api/orders/pay', {
      id: that.data.id,
      type: that.data.orderInfo.source,
      pay_type: type,
      token: that.data.formToken
    }).then(res => {
      that.setData({
        isSubmit: false,
      });

      if (res.code == 1) {
        // 微信支付
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
          success(res) {
            wx.showToast({
              title: '支付成功',
              duration: 2500,
              icon: 'success'
            });
            let url;
            if(that.data.orderInfo.source == 'recharge_money'){
              url = '/pages/users/rechargeslog/index';
            }else{
              url = '/pages/order/list/index?type=2';
            }
            setTimeout(function(){
              wx.navigateTo({
                url: url
              })
            }(url),2500);
          }
        })
      } else if (res.code == 11) {
        wx.showToast({
          title: '支付成功',
          duration: 2500,
          icon: 'success'
        });
        let url;
        if(that.data.orderInfo.source == 'recharge_money'){
          url = '/pages/users/rechargeslog/index';
        }else{
          url = '/pages/order/list/index?type=2';
        }
        setTimeout(function(){
          wx.navigateTo({
           url: url
          })
        }(url),2500);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2500,
        });
        that.setData({
          formToken: res.data.token
        });
      }
    })
  }
})